package cn.qqncn.furniture.service;

import cn.qqncn.furniture.mapper.FurnitureMapper;
import cn.qqncn.furniture.pojo.domain.Furniture;
import cn.qqncn.furniture.pojo.vo.PieVo;
import cn.qqncn.furniture.pojo.vo.ShopVo;
import cn.qqncn.furniture.util.OSSTool;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zkw
 * @ClassName FurnitureService.java
 * @Description 家具服务层
 * @createTime 2020年12月09日 08:57:00
 */
@Service
public class FurnitureService {
    @Resource
    private FurnitureMapper furnitureMapper;


    /**
     * 查询全部
     * @param page
     * @param size
     * @return
     */
    public List<Furniture> getAll(int page,int size){
        PageHelper.startPage(page,size); //开启分页插件
        List<Furniture> list = furnitureMapper.selectAll();
        return list;
    }



    /**
     * 名称模糊查询
     * @param page
     * @param size
     * @param post_search 模糊查询名称
     * @return
     */
    public List<ShopVo> pageSelect(int page,int size,String post_search,Integer min,Integer max){
        PageHelper.startPage(page,size);
        List<ShopVo> list = furnitureMapper.pageSelect(post_search,min,max);
        return list;
    }

    /**
     * 根据ID查询
     * @param furnitureId
     * @return
     */
    public Furniture getFurnitureById(Integer furnitureId){
        Furniture furniture = furnitureMapper.selectByPrimaryKey(furnitureId);
        return furniture;
    }

    /**
     * 添加家具商品
     * @param furniture
     * @return 商品自增id
     */
    public int addFurniture(Furniture furniture){
        int furnitureId = furnitureMapper.insert(furniture);
        return furnitureId;
    }

    /**
     * 查找所有家具
     * @return
     */
    public List<Furniture> searchAll(){
        List<Furniture> user1 = furnitureMapper.selectAll();
        return user1;
    }

    /**
     * 带有类别名的查询全部
     * @return
     */
    public List<ShopVo> getAllVo(){
        List<ShopVo> list = furnitureMapper.pageSelect("",1,1000000000);
        return list;
    }

    /**
     * 修改商品
     * @param furniture
     * @return
     */
    public int updateById(Furniture furniture){
        // 如果修改了商品图片，则删除旧的商品图片
        Furniture furniture1 = furnitureMapper.selectByPrimaryKey(furniture.getFurnitureId());
        if(furniture1.getFurnitureImg().equals(furniture.getFurnitureImg())==false){
            String[] split = furniture1.getFurnitureImg().split("aliyuncs.com/");
            OSSTool.delete(split[1]);
        }
        //修改商品
        int i = furnitureMapper.updateByPrimaryKey(furniture);
        return i;
    }

    /**
     * 多选删除
     * @param id
     * @return
     */
    public int deleteSelectFurniture(int[] id){
        for (int i = 0; i < id.length; i++) {
            Furniture furniture = furnitureMapper.selectByPrimaryKey(id[i]);
            String[] split = furniture.getFurnitureImg().split("aliyuncs.com/");
            OSSTool.delete(split[1]);
        }
        int i = furnitureMapper.deleteSelectFurniture(id);
        return i;
    }

    /**
     * 根据类别查询全部商品
     * @param categoryId
     * @return
     */
    public List<ShopVo> selectByCategoryId(Integer categoryId){
        List<ShopVo> shopVos = furnitureMapper.selectByCategoryId(categoryId);
        return shopVos;
    }

    /**
     * 查询各种类别商品的数量
     * @return list<PieVo>
     */
    public List<PieVo> selectPieVo(){
        return furnitureMapper.selectPieVo();
    }
}
